//
//  LifeCtrl.m
//  LaoWu
//
//  Created by yinqy on 15/4/23.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LifeCtrl.h"
#import "LifeItemCell.h"
#import "LifeDataTool.h"
#import "LifeWebViewCtrl.h"

@interface LifeCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *safeInfoArray;
@property(nonatomic,strong)NSMutableArray *lifeInfoArray;
@end

@implementation LifeCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"工友通"];
    self.safeInfoArray = [NSMutableArray array];
    self.lifeInfoArray = [NSMutableArray array];
    [self initLeftTableView];
    [self initRightTableView];
    [self.leftTableView headerBeginRefreshing];
    [self.rightTableView headerBeginRefreshing];
}

-(void)initLeftTableView{
    [self.leftTableView setDelegate:self];
    [self.leftTableView setDataSource:self];
     self.leftTableView.backgroundColor = PUBLIC_BG;
    [self.view addSubview: self.leftTableView];
    [self.leftTableView addHeaderWithTarget:self action:@selector(leftHeaderRefresh)];
    [self.leftTableView addFooterWithTarget:self action:@selector(leftFooterRefresh)];
    [self.leftTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 0)]];
}

-(void)initRightTableView{
    [self.rightTableView setDelegate:self];
    [self.rightTableView setDataSource:self];
    self.rightTableView.backgroundColor = PUBLIC_BG;
    [self.view addSubview: self.rightTableView];
    [self.rightTableView addHeaderWithTarget:self action:@selector(rightHeaderRefresh)];
    [self.rightTableView addFooterWithTarget:self action:@selector(rightFooterRefresh)];
    [ self.rightTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 0)]];
}

-(void)leftHeaderRefresh{
    [self getSafeDataWithMaxId:0];
}

-(void)leftFooterRefresh{
    NSInteger maxId = 0;
    if (self.safeInfoArray.count) {
        WorkReportItemResult *item = [self.safeInfoArray lastObject];
        maxId = item.id;
    }
    if(maxId > 0){
        maxId =  maxId-1;
    }
    [self getSafeDataWithMaxId:maxId];}

-(void)rightHeaderRefresh{
    [self getLifeDataWithMaxId:0];
}

-(void)rightFooterRefresh{
    NSInteger maxId = 0;
    if (self.lifeInfoArray.count) {
        WorkReportItemResult *item = [self.lifeInfoArray lastObject];
        maxId = item.id;
    }
    if(maxId > 0){
        maxId =  maxId-1;
    }
    [self getLifeDataWithMaxId:maxId];
}

-(void)getSafeDataWithMaxId:(NSInteger)maxId{
    [LifeDataTool getSafeInfoData:maxId success:^(SafeInfoRes *result) {
        [Utils refreshComplete:self.leftTableView];
        if(result.isSucc){
            if(maxId == 0){
                [self.safeInfoArray removeAllObjects];
            }
            [self.safeInfoArray addObjectsFromArray:result.datas];
             [self.leftTableView reloadData];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
         [Utils refreshComplete:self.leftTableView];
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
   
}

-(void)getLifeDataWithMaxId:(NSInteger)maxId{
    [LifeDataTool getLifeInfoData:maxId success:^(SafeInfoRes *result) {
        [Utils refreshComplete:self.rightTableView];
        if(result.isSucc){
            if(maxId == 0){
                [self.safeInfoArray removeAllObjects];
            }
            [self.lifeInfoArray addObjectsFromArray:result.datas];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.rightTableView];
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
}

- (IBAction)headerBtnClick:(id)sender {
    if (sender == self.leftBtn) {
        [self.leftBtn setSelected:YES];
        [self.rightBtn setSelected:NO];
        [self.leftTableView setHidden:NO];
        [self.rightTableView setHidden:YES];
        [self.leftTableView reloadData];
    }else if(sender == self.rightBtn){
        [self.rightBtn setSelected:YES];
        [self.leftBtn setSelected:NO];
        [self.leftTableView setHidden:YES];
        [self.rightTableView setHidden:NO];
        [self.rightTableView reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([self.leftBtn isSelected])
        return self.safeInfoArray.count;
    else
        return self.lifeInfoArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LifeItemCell *cell = [LifeItemCell cellWithTableView:tableView indexPath:indexPath];
    if([self.leftBtn isSelected])
        [cell initData:self.safeInfoArray[indexPath.row]];
    else
        [cell initData:self.lifeInfoArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 84;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.leftBtn isSelected]){
        [self.navigationController pushViewController:[LifeWebViewCtrl initWithUrl:[self.safeInfoArray[indexPath.row] content_url] title:@"安全知识"] animated:YES];
    }else{
        [self.navigationController pushViewController:[LifeWebViewCtrl initWithUrl:[self.lifeInfoArray[indexPath.row] content_url] title:@"生活知识"] animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
